{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## 花式打印序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def snake_square_sequence(n):\n",
    "    dat = [[0 for j in range(n)] for i in range(n)]\n",
    "    i, j = 0, 0\n",
    "    cur = 1\n",
    "\n",
    "    dat[i][j] = cur\n",
    "\n",
    "    while cur < n * n:\n",
    "        while j + 1 < n and dat[i][j + 1] == 0:\n",
    "            j += 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "        while i + 1 < n and dat[i + 1][j] == 0:\n",
    "            i += 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "        while j - 1 >= 0 and dat[i][j - 1] == 0:\n",
    "            j -= 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "        while i - 1 >= 0 and dat[i - 1][j] == 0:\n",
    "            i -= 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "\n",
    "    return '\\n'.join([''.join(['{:<4d}'.format(dat[i][j]) for j in range(n)]).rstrip() for i in range(n)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def snake_reverse_square_sequence(n):\n",
    "    dat = [[0 for j in range(n)] for i in range(n)]\n",
    "    i, j = 0 if n % 2 else (n - 1), (n - 1) if n % 2 else 0\n",
    "    cur = n * n\n",
    "\n",
    "    dat[i][j] = cur\n",
    "\n",
    "    while cur > 1:\n",
    "        while j + 1 < n and dat[i][j + 1] == 0:\n",
    "            j += 1\n",
    "            cur -= 1\n",
    "            dat[i][j] = cur\n",
    "        while i - 1 >= 0 and dat[i - 1][j] == 0:\n",
    "            i -= 1\n",
    "            cur -= 1\n",
    "            dat[i][j] = cur\n",
    "        while j - 1 >= 0 and dat[i][j - 1] == 0:\n",
    "            j -= 1\n",
    "            cur -= 1\n",
    "            dat[i][j] = cur\n",
    "        while i + 1 < n and dat[i + 1][j] == 0:\n",
    "            i += 1\n",
    "            cur -= 1\n",
    "            dat[i][j] = cur\n",
    "\n",
    "    return '\\n'.join([''.join(['{:<4d}'.format(dat[i][j]) for j in range(n)]).rstrip() for i in range(n)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def snake_diamond_sequence(n):\n",
    "    dat = [[0 for j in range(2 * n - 1)] for i in range(2 * n - 1)]\n",
    "    i, j = 0, n - 1\n",
    "    cur = 1\n",
    "\n",
    "    dat[i][j] = cur\n",
    "\n",
    "    while cur < n * n:\n",
    "        while j + 1 < 2 * n - 1 and dat[i + 1][j + 1] == 0:\n",
    "            i += 1\n",
    "            j += 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "        while i + 1 < 2 * n - 1 and dat[i + 1][j - 1] == 0:\n",
    "            i += 1\n",
    "            j -= 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "        while j - 1 >= 0 and dat[i - 1][j - 1] == 0:\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "        while i - 1 >= 0 and dat[i - 1][j + 1] == 0:\n",
    "            i -= 1\n",
    "            j += 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "\n",
    "    return '\\n'.join([''.join(['{:<2d}'.format(dat[i][j]) if dat[i][j] else ' ' * 2 for j in range(2 * n - 1)]).rstrip() for i in range(2 * n - 1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def snake_countable_sequence(n):\n",
    "    dat = [[0 for j in range(n)] for i in range(n)]\n",
    "    i, j = 0, 0\n",
    "    cur = 1\n",
    "\n",
    "    dat[i][j] = cur\n",
    "\n",
    "    while cur < n * n:\n",
    "        if j + 1 < n:\n",
    "            j += 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "        while i + 1 < n and j - 1 >= 0 and dat[i + 1][j - 1] == 0:\n",
    "            i += 1\n",
    "            j -= 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "        if i + 1 < n:\n",
    "            i += 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "        while i - 1 >= 0 and j + 1 < n and dat[i - 1][j + 1] == 0:\n",
    "            i -= 1\n",
    "            j += 1\n",
    "            cur += 1\n",
    "            dat[i][j] = cur\n",
    "\n",
    "    return '\\n'.join([''.join(['{:<4d}'.format(dat[i][j]) for j in range(n)]).rstrip() for i in range(n)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "square [3]:\n",
      "1   2   3\n",
      "8   9   4\n",
      "7   6   5\n",
      "\n",
      "square [4]:\n",
      "1   2   3   4\n",
      "12  13  14  5\n",
      "11  16  15  6\n",
      "10  9   8   7\n",
      "\n",
      "square [5]:\n",
      "1   2   3   4   5\n",
      "16  17  18  19  6\n",
      "15  24  25  20  7\n",
      "14  23  22  21  8\n",
      "13  12  11  10  9\n",
      "\n",
      "----------------------------------------\n",
      "\n",
      "reverse square [3]:\n",
      "7   8   9\n",
      "6   1   2\n",
      "5   4   3\n",
      "\n",
      "reverse square [4]:\n",
      "7   8   9   10\n",
      "6   1   2   11\n",
      "5   4   3   12\n",
      "16  15  14  13\n",
      "\n",
      "reverse square [5]:\n",
      "21  22  23  24  25\n",
      "20  7   8   9   10\n",
      "19  6   1   2   11\n",
      "18  5   4   3   12\n",
      "17  16  15  14  13\n",
      "\n",
      "----------------------------------------\n",
      "\n",
      "diamond [3]:\n",
      "    1\n",
      "  8   2\n",
      "7   9   3\n",
      "  6   4\n",
      "    5\n",
      "\n",
      "diamond [4]:\n",
      "      1\n",
      "    12  2\n",
      "  11  13  3\n",
      "10  16  14  4\n",
      "  9   15  5\n",
      "    8   6\n",
      "      7\n",
      "\n",
      "diamond [5]:\n",
      "        1\n",
      "      16  2\n",
      "    15  17  3\n",
      "  14  24  18  4\n",
      "13  23  25  19  5\n",
      "  12  22  20  6\n",
      "    11  21  7\n",
      "      10  8\n",
      "        9\n",
      "\n",
      "----------------------------------------\n",
      "\n",
      "countable [3]:\n",
      "1   2   6\n",
      "3   5   7\n",
      "4   8   9\n",
      "\n",
      "countable [4]:\n",
      "1   2   6   7\n",
      "3   5   8   13\n",
      "4   9   12  14\n",
      "10  11  15  16\n",
      "\n",
      "countable [5]:\n",
      "1   2   6   7   15\n",
      "3   5   8   14  16\n",
      "4   9   13  17  22\n",
      "10  12  18  21  23\n",
      "11  19  20  24  25\n",
      "\n",
      "----------------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "if __name__ == \"__main__\":\n",
    "    testcases = (\n",
    "        ('square', snake_square_sequence),\n",
    "        ('reverse square', snake_reverse_square_sequence),\n",
    "        ('diamond', snake_diamond_sequence),\n",
    "        ('countable', snake_countable_sequence),\n",
    "    )\n",
    "\n",
    "    for n, f in testcases:\n",
    "        for i in range(3, 6):\n",
    "            print('{} [{}]:'.format(n, i))\n",
    "            print(f(i))\n",
    "            print()\n",
    "        print('-' * 40, end='\\n\\n')"
   ]
  }
 ],
 "metadata": {
  "file_extension": ".py",
  "kernelspec": {
   "display_name": "Python 3.7.4 64-bit ('py37': conda)",
   "language": "python",
   "name": "python37464bitpy37conda124ef028e81f4ec7808674584596f802"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  },
  "version": 3
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
